package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class AddEventDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	String selected_staffer;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.addeventdialog);
        appState = ((CustomApp)getApplicationContext());
        /* get Staffer names */
        String staffer_names[] = new String[appState.GetContacts().size()];
    	for(int i = 0; i < appState.GetContacts().size(); i++)
    	{
    		if(!appState.GetContacts().get(i).get_position().equals("Chief Photographer")||
    				!appState.GetContacts().get(i).get_position().equals("Staff Photographer")){
    			staffer_names[i] = appState.GetContacts().get(i).get_name();
    		}
    	}
        Spinner spinner = (Spinner) findViewById(R.id.Get_staffer_covering_dropdown);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item,staffer_names);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new StafferSelectedListener());
    }
    
    public void okButtonHandler(View v){
    	/* gather the inputed information */
    	 EditText event_name = (EditText) findViewById(R.id.Get_event_name_text);
    	 String event_name_text = event_name.getText().toString();
    	 EditText event_date = (EditText) findViewById(R.id.Get_event_date_text);
    	 String event_date_text = event_date.getText().toString();
    	 EditText event_time = (EditText) findViewById(R.id.Get_event_time_text);
    	 String event_time_text = event_time.getText().toString();
    	 EditText contact_name = (EditText) findViewById(R.id.Get_contact_name_text);
    	 String contact_name_text = contact_name.getText().toString();
         EditText contact_phone = (EditText) findViewById(R.id.Get_contact_phone_text);
         String contact_phone_text = contact_phone.getText().toString();
         EditText location = (EditText) findViewById(R.id.Get_location_text);
         String location_text = location.getText().toString();
         EditText notes = (EditText) findViewById(R.id.Get_notes_text);
         String notes_text = notes.getText().toString();
         /* check if event already exists */
         for(int i = 0; i < appState.GetEvents().size(); i++){
        	 if(appState.GetEvents().get(i).get_event_name().equals(event_name_text)){
        		 Intent data = new Intent();
                 data.putExtra("exists", true);
        		 setResult(Activity.RESULT_OK, data);
        		 finish();
        	 }
         }
         /* build a new intent to return the data */
         Intent data = new Intent();
         data.putExtra("exists", false);
         data.putExtra("event_name", event_name_text);
         data.putExtra("event_date", event_date_text);
         data.putExtra("event_time", event_time_text);
         data.putExtra("contact_name", contact_name_text);
         data.putExtra("contact_phone", contact_phone_text);
         data.putExtra("staffer", selected_staffer);
         data.putExtra("location", location_text);
         data.putExtra("notes", notes_text);
         setResult(Activity.RESULT_OK, data);
    	 finish();
    }
    
    /* class that monitors the staffer selector */
    public class StafferSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	selected_staffer = parent.getItemAtPosition(pos).toString();

        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
    
    //need to be completed
    public void setSpinnerPosition(int pos){
    	
    }
    
    public void setSpinnerSelection(String select){
    	
    }
    
    public int getSpinnerPosition(){
    	return 0;
    }
    
    public String getSpinnerSelection(){
    	return null;
    }
    
}